package com.java110.mall.goods.bmo.assetInventory.impl;

import com.java110.dto.asset.AssetInventoryDto;
import com.java110.mall.goods.bmo.assetInventory.IGetAssetInventoryBMO;
import com.java110.intf.goods.IAssetInventoryInnerServiceSMO;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("getAssetInventoryBMOImpl")
public class GetAssetInventoryBMOImpl implements IGetAssetInventoryBMO {

    @Autowired
    private IAssetInventoryInnerServiceSMO assetInventoryInnerServiceSMOImpl;

    /**
     * @param assetInventoryDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> get(AssetInventoryDto assetInventoryDto) {


        int count = assetInventoryInnerServiceSMOImpl.queryAssetInventorysCount(assetInventoryDto);

        List<AssetInventoryDto> assetInventoryDtos = null;
        if (count > 0) {
            assetInventoryDtos = assetInventoryInnerServiceSMOImpl.queryAssetInventorys(assetInventoryDto);
            refreshReport(assetInventoryDtos);
        } else {
            assetInventoryDtos = new ArrayList<>();
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) assetInventoryDto.getRow()), count, assetInventoryDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        return responseEntity;
    }

    /**
     * 刷入数据
     *
     * @param assetInventoryDtos
     */
    private void refreshReport(List<AssetInventoryDto> assetInventoryDtos) {
        if (assetInventoryDtos == null || assetInventoryDtos.size() < 1) {
            return;
        }
        List<String> aiIds = new ArrayList<>();
        for (AssetInventoryDto assetInventoryDto : assetInventoryDtos) {
            aiIds.add(assetInventoryDto.getAiId());
        }
        AssetInventoryDto assetInventoryDto = new AssetInventoryDto();
        assetInventoryDto.setAiIds(aiIds.toArray(new String[aiIds.size()]));
        assetInventoryDto.setShopId(assetInventoryDtos.get(0).getShopId());
        List<AssetInventoryDto> tmpAssetInventoryDtos = assetInventoryInnerServiceSMOImpl.queryAssetInventoryReports(assetInventoryDto);

        if (tmpAssetInventoryDtos == null || tmpAssetInventoryDtos.size() < 1) {
            return;
        }

        for(AssetInventoryDto tAssetInventoryDto : assetInventoryDtos){
            for(AssetInventoryDto tmpAssetInventoryDto : tmpAssetInventoryDtos){
                if(!tAssetInventoryDto.getAiId().equals(tmpAssetInventoryDto.getAiId())){
                    continue;
                }
                tAssetInventoryDto.setInvQuantity(tmpAssetInventoryDto.getInvQuantity());
                tAssetInventoryDto.setQuantity(tmpAssetInventoryDto.getQuantity());
                tAssetInventoryDto.setQuantityMoney(tmpAssetInventoryDto.getQuantityMoney());
                tAssetInventoryDto.setInvQuantityMoney(tmpAssetInventoryDto.getInvQuantityMoney());
            }
        }
    }

}
